package com.jincheng.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jincheng.system.domain.Comment;
import com.jincheng.system.domain.CommentLike;
import com.jincheng.system.domain.dto.CommentDTO;
import com.jincheng.system.mapper.CommentLikeMapper;
import com.jincheng.system.mapper.CommentMapper;
import com.jincheng.system.mapstruct.CommentMapStruct;
import com.jincheng.system.service.ICommentLikeService;
import com.jincheng.system.service.ICommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.jincheng.common.utils.SecurityUtils.getUserId;

/**
 * 评论Service业务层处理
 *
 * @author DairyJay
 * @date 2025-03-02
 */
@RequiredArgsConstructor
@Service
public class CommentServiceImpl implements ICommentService {
    private final CommentMapper commentMapper;
    private final CommentMapStruct commentMapStruct;
    private final CommentLikeMapper commentLikeMapper;
    private final ICommentLikeService commentLikeServiceImpl;

    /**
     * 查询评论
     *
     * @param commentId 评论主键
     * @return 评论
     */
    @Override
    public Comment selectCommentByCommentId(Long commentId) {
        return commentMapper.selectCommentByCommentId(commentId);
    }

    /**
     * 查询评论列表
     *
     * @param comment 评论
     * @return 评论
     */
    @Override
    public List<Comment> selectCommentList(Comment comment) {
        return commentMapper.selectCommentList(comment);
    }

    /**
     * 根据活动id查询评论
     *
     * @param activityId 活动id
     * @return 评论集合
     */
    @Override
    public List<CommentDTO> selectCommentListByActivityId(Long activityId) {
        return commentMapper.selectCommentListByActivityId(activityId, getUserId());
    }

    /**
     * 新增评论
     *
     * @param comment 评论
     * @return 结果
     */
    @Override
    public int insertComment(Comment comment) {
        return commentMapper.insert(comment);
    }

    /**
     * 修改评论
     *
     * @param comment 评论
     * @return 结果
     */
    @Override
    public int updateComment(Comment comment) {
        return commentMapper.update(new LambdaUpdateWrapper<Comment>()
                .eq(Comment::getCommentId, comment.getCommentId())
                .eq(Comment::getActivityId, comment.getActivityId())
                .set(comment.getUserId() != null,
                        Comment::getUserId, comment.getUserId())
                .set(comment.getContent() != null,
                        Comment::getContent, comment.getContent())
                .set(comment.getStatus() != null,
                        Comment::getStatus, comment.getStatus())
                .set(comment.getLikes() != null,
                        Comment::getLikes, comment.getLikes())
                .set(comment.getRemark() != null,
                        Comment::getRemark, comment.getRemark())
        );
    }

    /**
     * 批量删除评论
     *
     * @param commentIds 需要删除的评论主键
     * @return 结果
     */
    @Override
    public int deleteCommentByCommentIds(Long[] commentIds) {
        return commentMapper.deleteCommentByCommentIds(commentIds);
    }

    /**
     * 删除评论信息
     *
     * @param commentId 评论主键
     * @return 结果
     */
    @Override
    public int deleteCommentByCommentId(Long commentId) {
        return commentMapper.deleteCommentByCommentId(commentId);
    }

    /**
     * 发表评论
     *
     * @param comment 评论信息
     * @return 结果
     */
    @Override
    public int publishComment(Comment comment) {
        comment.setUserId(getUserId());
        return insertComment(comment);
    }


    /**
     * 修改评论点赞信息
     *
     * @param commentDTO 评论点赞信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCommentAndLike(CommentDTO commentDTO) {
        commentDTO.setCreateTime(null);

        // 修改评论信息
        Comment comment = commentMapStruct.toComment(commentDTO);
        int commentRows = updateComment(comment);

        // 修改/新增点赞信息
        CommentLike commentLike = commentMapStruct.toCommentLike(commentDTO);
        int commentLikeRows = commentLikeServiceImpl.saveOrUpdate(commentLike);

        return Math.min(commentRows, commentLikeRows);
    }
}
